package com.ssyc.gsk_teacher_appraisal.adapter;

import android.content.Context;
import android.widget.TextView;

import com.ssyc.common.adapter.CommonAdapter;
import com.ssyc.common.adapter.ViewHolder;
import com.ssyc.gsk_teacher_appraisal.R;
import com.ssyc.gsk_teacher_appraisal.bean.ReadLvInfo;

import java.util.List;

/**
 * 展示答案选项的适配器
 * Created by Administrator on 2018/9/4 0004.
 */

public class ReadChooseAnswerAdapter extends CommonAdapter<ReadLvInfo> {
    private Context context;
    private List<ReadLvInfo> datas;
    //是否是老师的标识,0是学生,1是老师
    private int isTeacher = -1;
    //老师端进入时是否需要显示学生选择的选项,0不需要显示,1需要显示
    private int isShowStudentChooseAnswer = -1;

    //提供给老师端的构造函数
    public ReadChooseAnswerAdapter(int isTeacher, int isShowStudentChooseAnswer, Context context, List<ReadLvInfo> datas, int layoutId) {
        super(context, datas, layoutId);
        this.context = context;
        this.datas = datas;
        this.isTeacher = isTeacher;
        this.isShowStudentChooseAnswer = isShowStudentChooseAnswer;
    }

    //提供给学生端的构造函数
    public ReadChooseAnswerAdapter(int isTeacher, Context context, List<ReadLvInfo> datas, int layoutId) {
        super(context, datas, layoutId);
        this.context = context;
        this.datas = datas;
        this.isTeacher = isTeacher;
    }

    @Override
    public void convert(ViewHolder holder, ReadLvInfo readLvInfo) {
        //索引值A--F
        TextView tvIndex = holder.getView(R.id.tv_index);
        tvIndex.setText(readLvInfo.questionIndex);

        //答案内容
        TextView tvAnswer = holder.getView(R.id.tv_content);
        tvAnswer.setText(readLvInfo.questionContent);

        if (isTeacher == 0) {
            //学生端
            //是否选中
            if (readLvInfo.isSelected) {
                //选中
                tvIndex.setTextColor(context.getResources().getColor(R.color.tv_app_tv_select));
                tvIndex.setBackgroundResource(R.drawable.teacher_class_tv_select);
            } else {
                //未选中
                tvIndex.setTextColor(context.getResources().getColor(R.color.tv_app_tv_normal));
                tvIndex.setBackgroundResource(R.drawable.teacher_class_tv_normal);
            }
        } else if (isTeacher == 1) {
            //老师端,2种情况 1种:查看某个学生成绩的情况,此时需要显示学生选择的正确选项
            //2种:查看试卷和预览试卷,此时只需要展示该题的正确答案选项
            if (isShowStudentChooseAnswer == 1) {
                //需要展示学生选择的正确答案

                if (readLvInfo.questionIndex.equals(readLvInfo.chooseAnswer)) {
                    //如果当前项是用户选择的答案,进行用户颜色处理,标红
                    tvIndex.setTextColor(context.getResources().getColor(R.color.tv_app_tv_select));
                    tvIndex.setBackgroundResource(R.drawable.teacher_class_tv_use_choose);
                } else {
                    //不是用户选择的项
                    tvIndex.setTextColor(context.getResources().getColor(R.color.tv_app_tv_normal));
                    tvIndex.setBackgroundResource(R.drawable.teacher_class_tv_normal);
                }

                //如果当前项不是用户选择的答案
                if (readLvInfo.questionIndex.equals(readLvInfo.rightAnswer)) {
                    //如果当前项是正确选项
                    tvIndex.setTextColor(context.getResources().getColor(R.color.tv_app_tv_select));
                    tvIndex.setBackgroundResource(R.drawable.teacher_class_tv_right_answer);
                }

            } else if (isShowStudentChooseAnswer == 0) {
                //不需要展示学生的正确答案,只将正确答案进行标记
                if (readLvInfo.questionIndex.equals(readLvInfo.rightAnswer)) {
                    //如果当前项是正确选项
                    tvIndex.setTextColor(context.getResources().getColor(R.color.tv_app_tv_select));
                    tvIndex.setBackgroundResource(R.drawable.teacher_class_tv_right_answer);
                } else {
                    //如果当前项不是正确答案
                    tvIndex.setTextColor(context.getResources().getColor(R.color.tv_app_tv_normal));
                    tvIndex.setBackgroundResource(R.drawable.teacher_class_tv_normal);
                }
            }
        }
    }

    /**
     * 同步状态
     *
     * @param pos
     */
    public void upDateStete(int pos) {

        for (int i = 0; i < datas.size(); i++) {
            if (i == pos) {
                datas.get(i).isSelected = true;
            } else {
                datas.get(i).isSelected = false;
            }
        }
        notifyDataSetChanged();
    }
}
